package com.eliza.databindadv.twoWay

import android.text.TextUtils
import android.util.Log
import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import androidx.databinding.ObservableField
import com.eliza.databindadv.BR
import com.eliza.databindadv.model.User


/*-*- coding:utf-8 -*-
 * @Author  : debi
 * @Time    : 3/26/22
 * @Software: Android Studio
 */

/*
单项绑定
* Field --> 字段变化 TextView 自动更新

双向绑定
* * Field --> 字段变化 TextView 自动更新
* * Field <--- 用户编辑 editText 字段自动同步 《-- TextView


* */
/**
 * BaseObservable 与 ObservableField
 */
class UserVmObservableField : BaseObservable {
    var userVo: ObservableField<User> = ObservableField<User>()

    constructor() {
        userVo.set(User("init", 0))
    }

    fun getUserName(): String {
        return userVo.get()?.userName!!
    }

    //    https://www.bilibili.com/video/BV1Ry4y1t7Tj?p=13&spm_id_from=pageDriver
    fun setUserName(userName: String) {
        if (!TextUtils.isEmpty(userName) && userVo.get()?.userName != userName) {
            userVo.get()?.userName = userName
            Log.e(this::class.java.name, "setUserName-------$userName")

        }

    }

}